package abio.XasML.builder
{
	import abio.XasML.builder.XasML.declear;
	import abio.XasML.rpc.classLoader;
	import abio.XasML.rpc.command;
	import abio.XasML.rpc.commandChain;
	
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	import mx.rpc.events.ResultEvent;
	
	import org.spicefactory.lib.reflect.ClassInfo;
	import org.spicefactory.lib.reflect.Property;
	
	internal class objectBuilder
	{
		public function objectBuilder()
		{
		}

		static internal function build(builder:Object, cntxt:context):Object{
			var cmdChain:commandChain=new commandChain(commandChain.PARALLEL);
			for each(var tempProperty:Property in ClassInfo.forInstance(builder).getProperties()){
				if(tempProperty.getValue(builder) is declear){
					var tempDeclear:declear=tempProperty.getValue(builder) as declear;
					if(tempDeclear.type is String){
						if(tempDeclear.from==null){
							tempDeclear.type=getDefinitionByName(tempDeclear.type as String);
						}else{
							var tempFunc:Function=cmdChain.add;
							var tempArgs:Array=[new command(new classLoader(tempDeclear.from)[tempDeclear.type],function onResult(re:ResultEvent):void{
								trace('sl2', re.result);
							})];
							tempFunc.apply(cmdChain,tempArgs.concat(tempDeclear.constructorArgs));					
						}
					}
				}
			}
			cmdChain.proceed(function onResult():void{
				cntxt.dispatchEvent(new Event(Event.COMPLETE));
			});
			return builder;
		}
	}
}